package com.wlcorp.forcazamis.web.action.confirm;

import java.sql.Date;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.confirm.IConfirmService;
import com.wlcorp.forcazamis.domain.confirm.Confirm;
import com.wlcorp.forcazamis.domain.confirm.ConfirmDetail;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.Pagination;
import com.wlcorp.forcazamis.sys.util.SessionValue;
import com.wlcorp.forcazamis.sys.util.Validator;
import com.wlcorp.forcazamis.sys.util.WebUtil;

public class ConfirmAction extends DispatchAction {
	public ActionForward getConfirmList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Confirm confirm = new Confirm();
		confirm.setNoState(AppConstant.CONFIRM_STATE_CONFIRMED);
		confirm.setNextConfirmerId(SessionValue.getInstance(request).getStaff().getStaffId());
		WebUtil.setRequestToObject(request,confirm);
		IConfirmService service = ContextUtil.getInstance().getConfirmService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getConfirmCount(confirm).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    confirm.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
		List<Confirm> confirmList = service.getConfirmList(confirm);
		request.setAttribute("confirmList",confirmList);
		request.setAttribute("title","待审批清单");
		return mapping.findForward("list");
	}
	
	public ActionForward getConfirmHistory(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Confirm confirm = new Confirm();
		confirm.setState(AppConstant.CONFIRM_STATE_CONFIRMED);
		WebUtil.setRequestToObject(request,confirm);
		IConfirmService service = ContextUtil.getInstance().getConfirmService();
		int page = 1;
		int count = 0;
	    if (null != request.getParameter(Pagination.PAGE_ATTRIBUTE)) {
	      page = Integer.parseInt(request.getParameter(Pagination.
	          PAGE_ATTRIBUTE));
	    }
	    count = (int)service.getConfirmCount(confirm).longValue();
	    Pagination pagination = new Pagination();
	    pagination.setPageCount(Pagination.PAGE_RECORD_LINE_NUM);
	    pagination.generatePageJSPFragment(request, count);
	    confirm.setPage(page, Pagination.PAGE_RECORD_LINE_NUM);
		List<Confirm> confirmList = service.getConfirmList(confirm);
		request.setAttribute("confirmList",confirmList);
		request.setAttribute("title","历史审批记录");
		return mapping.findForward("list");
	}
	
	public ActionForward getConfirm(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		
		Confirm confirm = new Confirm();
		WebUtil.setRequestToObject(request,confirm);
		confirm = ContextUtil.getInstance().getConfirmService().getConfirmById(confirm.getConfirmId());
		if(null == confirm){
			confirm = new Confirm();
		}
		request.setAttribute("item",ContextUtil.getInstance().getConfirmService().getItem(confirm.getItemId(),confirm.getConfirmType()));
		request.setAttribute("confirm",confirm);
		return mapping.findForward(getConfirmDetailModifyPath(confirm.getConfirmType()));
	}
	
	public ActionForward submitConfirm(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		WebUtil.setResponseNoCache(response);
		Staff currentStaff = ContextUtil.getInstance().getStaffService().getStaffById(SessionValue.getInstance(request).getStaff().getStaffId());
		Confirm confirm = new Confirm();
		String confirmId = request.getParameter("confirmId");
		if(Validator.checkValueIsValid(confirmId)){
			confirm = ContextUtil.getInstance().getConfirmService().getConfirmById(Long.valueOf(confirmId));
		}else{
			confirm.setSubmiter(currentStaff);
			confirm.setSubmitTime(new Date(Calendar.getInstance().getTimeInMillis()));
			confirm.setState(AppConstant.CONFIRM_STATE_NEW);
		}
		WebUtil.setRequestToObject(request,confirm);
		ContextUtil.getInstance().getConfirmService().saveConfirm(confirm);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == confirm.getStatus()){
			WebUtil.printText(response,confirm.getMsg());
		}else{
			WebUtil.printText(response,"审批请求已提交！");
		}
		
		return null;
	}
	
	public ActionForward submitConfirmDetail(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		ConfirmDetail confirmDetail = new ConfirmDetail();
		WebUtil.setRequestToObject(request,confirmDetail);
		confirmDetail.setConfirmer(SessionValue.getInstance(request).getStaff());
		confirmDetail.setConfirmDate(new Date(Calendar.getInstance().getTimeInMillis()));
		ContextUtil.getInstance().getConfirmService().saveConfirmDetail(confirmDetail);
		if(AppConstant.BASE_BEAN_STATUS_ERROR == confirmDetail.getStatus()){
			WebUtil.setAlertMessage(request,confirmDetail.getMsg());
		}else{
			WebUtil.setAlertMessage(request,"审批成功！");
		}
		Confirm confirm = ContextUtil.getInstance().getConfirmService().getConfirmById(confirmDetail.getConfirmId());
		request.setAttribute("confirm",confirm);
		request.setAttribute("item",ContextUtil.getInstance().getConfirmService().getItem(confirm.getItemId(),confirm.getConfirmType()));
		return mapping.findForward(getConfirmDetailModifyPath(confirm.getConfirmType()));
	}
	
	private String getConfirmDetailModifyPath(Integer confirmType){
		String path = "";
		if(AppConstant.CONFIRM_TYPE_IN_STORAGE == confirmType){
			path = "storageInConfirmModify";
		}else if(AppConstant.CONFIRM_TYPE_OUT_STORAGE == confirmType){
			path = "storageOutConfirmModify";
		}else if(AppConstant.CONFIRM_TYPE_ASSIGN_STORAGE == confirmType){
			path = "storageAssignConfirmModify";
		}else if(AppConstant.CONFIRM_TYPE_PI == confirmType){
			path = "pidetailConfirmModify";
		}else if(AppConstant.CONFIRM_TYPE_STOCK == confirmType){
			path = "stockConfirmModify";
		}
		return path;
	}
	
}
